#ifndef SPHERE_H_
#define SPHERE_H_

#include <limits>
#include "Object.h"

using namespace std;

class Sphere :
	public Object
{
public:
	Sphere(Vec3 c = Vec3(0, 0, 10), float r = 1)
		: _center(c), _radius(r)
	{}
	virtual ~Sphere(void){};

	Vec3 _center;
	float _radius;

	float Intersect(const Ray & ray)
	{
		Vec3 dist = ray._origin - _center;
		Vec3 dir = ray._direction;
		//float A = dir*dir;
		float B = (dist*dir);
		float C = (dist*dist) - _radius*_radius;
		float delta = B*B - C;
		if(delta < 0.0f)
			return numeric_limits<float>::infinity();

		return (- B - sqrtf(delta));

		// commented out due to backface-culling :P
		// float t2 = (- B + sqrtDelta);

	//	if(t1 > 0.0f && t2 > 0.0f)
	//		return min(t1, t2);
	//	if(t1 > 0.0f)
	//		return t1;
	//	if(t2 > 0)
	//		return t2;
	//	return numeric_limits<float>::infinity();
	}

	virtual IntersectionInfo GetIntersectionInfo(const Vec3 & point)
	{
		return IntersectionInfo(point - _center, &_material);
	}
};

#endif
